package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2025-02-19
 * Time: 20:06
 */
// 单例模式(懒汉模式)

class SingletonLazy {
    private static volatile SingletonLazy instance = null;
    //instance是null, 就说明是首次调用, 就需要加锁. 如果是非null, 就说明是后续调用,就不需要加锁
    public static SingletonLazy getInstance() {
        if (instance == null) {
            synchronized (SingletonLazy.class) {
                if (instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }

    private SingletonLazy() {}
}

public class Demo17 {
    public static void main(String[] args) {
        SingletonLazy s1 = SingletonLazy.getInstance();
        SingletonLazy s2 = SingletonLazy.getInstance();
        System.out.println(s1 == s2);
    }
}
